/// 登录页面
part of pda.page;

class PageLogin extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PageLoginState();
  }
}

class _PageLoginState extends State<PageLogin> {
  Size screenSize;
  final formKey = GlobalKey<FormState>();
  bool maskPassword = true;
  TextEditingController userTxtController = TextEditingController();
  TextEditingController passwordTxtController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (screenSize == null) {
      screenSize = MediaQuery.of(context).size;
    }
    return Scaffold(
      appBar: AppBar(
        title: Text('登录'),
      ),
      body: Column(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
            child: userTextField(),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
            child: passwordTextField(),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 8, vertical: 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                FlatButton.icon(
                  icon: Icon(Icons.settings),
                  label: Text('设置'),
                  onPressed: () {
                    Navigator.of(context).pushNamed('/setting');
                  },
                ),
                FlatButton.icon(
                  icon: Icon(Icons.file_download),
                  label: Text('下载'),
                  onPressed: () {
                    Navigator.of(context).pushNamed('/download');
                  },
                ),
                FlatButton.icon(
                  // check_box 和 check_box_outline_blank 切换
                  icon: Icon(Icons.check_box),
                  // icon: Icon(Icons.check_box_outline_blank),
                  label: Text('在线使用'),
                  onPressed: () {},
                )
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.all(8),
            child: Row(
              // mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    child: Container(
                      padding: EdgeInsets.all(8),
                      child: Text(
                        '登录',
                        style: TextStyle(fontSize: 24),
                      ),
                    ),
                    textColor: Colors.white,
                    color: Colors.blue,
                    onPressed: login,
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget userTextField() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        Container(padding: EdgeInsets.only(right: 8.0), child: Text('用户:')),
        Expanded(
          child: TextField(
            controller: userTxtController,
            decoration: InputDecoration(
                hintText: '请输入用户名',
                suffixIcon: IconButton(
                  icon: Icon(Icons.remove_circle),
                  onPressed: () {
                    userTxtController.clear();
                  },
                )),
          ),
        )
      ],
    );
  }

  Widget passwordTextField() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        Container(padding: EdgeInsets.only(right: 8.0), child: Text('密码:')),
        Expanded(
          child: TextField(
            controller: passwordTxtController,
            obscureText: maskPassword,
            decoration: InputDecoration(
                hintText: '请输入密码',
                suffixIcon: IconButton(
                  icon: Icon(Icons.remove_red_eye),
                  onPressed: () {
                    setState(() {
                      maskPassword = !maskPassword;
                    });
                  },
                )),
          ),
        )
      ],
    );
  }

  void login() {
    if (userTxtController.text.isEmpty) {
      showAlert(context, '请输入用户名');
      return;
    }
    User user =
        User(id: userTxtController.text, password: passwordTxtController.text);
    user.login().then((user) {
      if (user != null) {
        Store.set('me', user);
        Navigator.pushNamedAndRemoveUntil(context, '/home', (router) {
          return router.settings.name == '/home';
        });
      } else {
        showError(context, '用户名或密码错误');
      }
    });
  }
}
